package com.yanqu.road.server.manger.activity.bowart;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.bowart.*;
import com.yanqu.road.entity.activity.bowart.enums.eTargetType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.BowArtBusiness;
import com.yanqu.road.pb.activity.BowArtProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.bowart.BowArtModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BowArtMgr extends TempMgr {

    public static final int CONDITION_TYPE_SERVER_UNION = 1029;
    public static final int CONDITION_TYPE_CROSS_UNION = 1030;

    private static ActivityInfo activityInfo;

    private static List<TargetBehavior> targetBehaviorList;

    //移动速度(一周期需要的时间：毫秒)
    private static List<Integer> BOW_ART_MOVE_CYCLE_TIME;
    //最大距离偏移
    private static List<Integer> BOW_ART_MAX_OFFSET_DISTANCE;
    //箭靶难度
    private static List<Integer> BOW_ART_TARGET_DIFFICULTY;
    //横杆长度
    public static int TRANSVERSE_LENGTH = 870;
    //纵杆长度
    public static int VERTICAL_LENGTH = 500;
    //圆半径
    public static int CIRCLE_RADIUS = 405;
    //蛇形圆半径
    public static double SERPENTINE_RADIUS = 256.5;
    //箭靶环数半径
    private static List<BowArtJianBaRingInfo> jianBaScoreInfoList;
    //连续命中圆心
    private static List<BowArtMingZhongScoreInfo> mingZhongScoreInfoList;


    //木剑道具id
    public static int BOW_ART_MU_JIAN_ID = 120300;
    //铁剑道具id
    public static int BOW_ART_TIE_JIAN_ID = 120301;
    //柳叶箭道具id
    public static int BOW_ART_LIU_YE_JIAN_ID = 120302;
    //将军箭道具id
    public static int BOW_ART_JIANG_JUN_JIAN_ID = 120303;
    //轩辕箭道具id
    public static int BOW_ART_XUAN_YUAN_JIAN_ID = 120307;

    public static int BOW_ART_SMELT_OPEN_VALUE = 50000;

    public static int BOW_ART_SCORE_GOODS_ID = 101939;

    public static int BOW_ART_BAIYE_RATIO = 5;

    @Override
    public boolean reloadData() throws Exception {
        initTargetBehavior();
        return true;
    }

    private void initTargetBehavior() {
        targetBehaviorList = new ArrayList<>();
        targetBehaviorList.add(new TargetBehavior(0, 0, eTargetType.Fixed.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(0, 0, eTargetType.Fixed.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(0, TRANSVERSE_LENGTH / 2, eTargetType.Transverse.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(1, VERTICAL_LENGTH / 2, eTargetType.Vertical.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(1, SERPENTINE_RADIUS, eTargetType.Serpentine.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(1, CIRCLE_RADIUS, eTargetType.Circular.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(2, 0, eTargetType.Fixed.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(2, TRANSVERSE_LENGTH / 2, eTargetType.Transverse.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(2, CIRCLE_RADIUS, eTargetType.Circular.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(2, SERPENTINE_RADIUS, eTargetType.Serpentine.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(3, 0, eTargetType.Fixed.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(3, VERTICAL_LENGTH / 2, eTargetType.Vertical.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(3, SERPENTINE_RADIUS, eTargetType.Serpentine.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(3, CIRCLE_RADIUS, eTargetType.Circular.getIntValue()));
        targetBehaviorList.add(new TargetBehavior(3, CIRCLE_RADIUS, eTargetType.Circular.getIntValue()));
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 是否在清空道具时间内
     */
    public static boolean isActivityInClearTime() {
        return ActivityMgr.activityInShowPeriod(activityInfo) || ActivityMgr.activityInRewardPeriod(activityInfo);
    }

    /**
     * 获取展示中的活动ID（0表示没活动）
     */
    public static int getShowTimeActivityId() {
        return activityInfo == null ? 0 : activityInfo.getActivityId();
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        BowArtMgr.activityInfo = activityInfo;
    }

    public static boolean activityInTime(){
        return ActivityMgr.activityInTime(activityInfo);
    }

    public static void reloadActivityData() {
        getLogger().info("reload bow art activity start");

        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.BowArtActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);

        Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        //配置
        BOW_ART_MAX_OFFSET_DISTANCE = StringUtils.stringToIntegerList(configMap.get("BOW_ART_MAX_OFFSET_DISTANCE").getValue(), "\\|");
        BOW_ART_TARGET_DIFFICULTY = StringUtils.stringToIntegerList(configMap.get("BOW_ART_TARGET_DIFFICULTY").getValue(), "\\|");
        BOW_ART_MOVE_CYCLE_TIME = StringUtils.stringToIntegerList(configMap.get("BOW_ART_MOVE_CYCLE_TIME").getValue(), "\\|");
        //熔炼开启条件
        BOW_ART_SMELT_OPEN_VALUE = Integer.valueOf(configMap.get("BOW_ART_SMELT_OPEN_VALUE").getValue());
        BOW_ART_BAIYE_RATIO = configMap.get("BOW_ART_BAIYE_RATIO").getIntValue();
        List<BowArtJianBaRingInfo> tempJianBaScoreInfoList = BowArtBusiness.getBowArtJianBaScoreInfo(tmpActivityInfo.getActivityId());

        List<BowArtMingZhongScoreInfo> tempMingZhongScoreInfoList = BowArtBusiness.getBowArtMingZhongScoreInfo(tmpActivityInfo.getActivityId());

        //水果道具id
        List<Integer> param3List = StringUtils.stringToIntegerList(tmpActivityInfo.getParam3(), "\\|");
        if(param3List.size() == 5){
            BOW_ART_MU_JIAN_ID = param3List.get(0);
            BOW_ART_TIE_JIAN_ID = param3List.get(1);
            BOW_ART_LIU_YE_JIAN_ID = param3List.get(2);
            BOW_ART_JIANG_JUN_JIAN_ID = param3List.get(3);
            BOW_ART_XUAN_YUAN_JIAN_ID = param3List.get(4);
        }

        BOW_ART_SCORE_GOODS_ID = Integer.valueOf(tmpActivityInfo.getParam2());

        jianBaScoreInfoList = tempJianBaScoreInfoList;
        mingZhongScoreInfoList = tempMingZhongScoreInfoList;
        activityInfo = tmpActivityInfo;

        reloadPlayerData();
        getLogger().info("reload bow art activity end");
    }

    private static void reloadPlayerData() {
        if (ActivityMgr.activityInShowTime(activityInfo)) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.BowArtActivity.getValue())) {
                    BowArtModule module = player.getModule(BowArtModule.class);
                    module.dailyReset();
                    if (module.getUserData() == null || module.getUserData().getActivityId() != activityInfo.getActivityId()) {
                        module.initUserData();
                        module.syncUserData();
                    }
                }
            }
        }
    }

    /**
     * 获取最大偏移量
     */
    public static int getMaxOffsetPixel(int distanceIdx){
        return BOW_ART_MAX_OFFSET_DISTANCE.get(distanceIdx);
    }

    public static int getDifficulty(int targetType){
        return BOW_ART_TARGET_DIFFICULTY.get(targetType);
    }
    /**
     * 获取下一个箭靶
     */
    public static int getNextTargetBehavior(int curIndex){
        if(curIndex < 0){
            return 0;
        }
        if(curIndex == targetBehaviorList.size() - 1){
            return 0;
        }
        return curIndex + 1;
    }


    public static BowArtJianBaRingInfo getJianBaRingConfig(double eyeDistance) {
        for(BowArtJianBaRingInfo scoreInfo : jianBaScoreInfoList){
            if(eyeDistance >= scoreInfo.getRadius().get(0) && eyeDistance <= scoreInfo.getRadius().get(1)){
                return scoreInfo;
            }
        }
        return null;
    }

    public static BowArtMingZhongScoreInfo getBowArtMingZhongScoreInfo(int contiRings){
        for(BowArtMingZhongScoreInfo scoreInfo : mingZhongScoreInfoList){
            if(contiRings >= scoreInfo.getNum().get(0) && contiRings <= scoreInfo.getNum().get(1)){
                return scoreInfo;
            }
        }
        return null;
    }

    public static BowArtProto.BowArtMingZhongEyeScoreTemp.Builder getMingZhongScoreBuilder(BowArtMingZhongScoreInfo mingZhongScoreInfo){
        BowArtProto.BowArtMingZhongEyeScoreTemp.Builder builder = BowArtProto.BowArtMingZhongEyeScoreTemp.newBuilder();
        builder.setId(mingZhongScoreInfo.getId());
        builder.setName(mingZhongScoreInfo.getName());
        builder.setScore(mingZhongScoreInfo.getIntegral());
        builder.addAllNumsRange(mingZhongScoreInfo.getNum());
        return builder;
    }

    public static BowArtProto.BowArtJianBaRingScoreTemp.Builder getJianBaRingScoreBuilder(BowArtJianBaRingInfo jianBaScoreInfo){
        BowArtProto.BowArtJianBaRingScoreTemp.Builder builder = BowArtProto.BowArtJianBaRingScoreTemp.newBuilder();
        builder.setId(jianBaScoreInfo.getId());
        builder.setName(jianBaScoreInfo.getName());
        builder.setScore(jianBaScoreInfo.getIntegral());
        builder.addAllRadiusRange(jianBaScoreInfo.getRadius());
        return builder;
    }

    public static BowArtProto.BowArtUserDataTemp.Builder getUserDataBuilder(BowArtUserData userData) {
        BowArtProto.BowArtUserDataTemp.Builder builder = BowArtProto.BowArtUserDataTemp.newBuilder();
        builder.setTotalScore(userData.getTotalScore());
        builder.setContiRings(userData.getContiRings());
        builder.setPatronsId(userData.getPatronsId());
        return builder;
    }

    public static List<TargetBehavior> getTargetBehaviorList() {
        return targetBehaviorList;
    }

    public static List<Integer> getBowArtMoveCycleTime() {
        return BOW_ART_MOVE_CYCLE_TIME;
    }

    public static List<Integer> getBowArtMaxOffsetDistance() {
        return BOW_ART_MAX_OFFSET_DISTANCE;
    }


}
